package com.example.petstore.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;

import java.util.HashMap;
import java.util.Map;

public class ZFBqrUtil {
    private static String app_id = "9021000134696463";
    private static String private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDBpmtNQn9h8Pd+veEX8eOrGb2vop82rogjFuzl6SRnJUwxdIZjhYc5Gk9IeEO6zVXqWzoz+VvPkVht6OFXBReBPZUVEuN8QMXSqDBwc5s/i8YinRa0rkRW/lPUiTF3UrpK0UaMrQx5/gulvMpCVOZUhU2HIJ/bRg3yLdmqdsjM1m6aUz+mAnEoTc0TltCvaupk1rWlUkA06mLJk3MtxxiQ71FPmqO3gdgLEh3Uh9CiCKX+Fob9/IEHBbaPHPiho4mW7gzxGNcGXff9O2MCv7cx4/2Aw+mIRCQyuOISE0SMRjIPFfJrIVT6SFtEhpJaPnP+9DHbj9d+Gk7/B/tscslTAgMBAAECggEBAMEl00qZM+KlbMXub8/pRRKItyiyhCkPRdvx5tGei9+RVQDS36PuMCSleESpJ6zvV21MFq52XbuXleLyb6fS/VG78bG2SE3LcUSe2smelU95g2NM2bPXSXSXcVfAiyUfz44GVF7Fz3byC/4AYqBse9v7pYtM4ATeR9SxJLSgi8G5tp0F62sejb+BJU4Jzg4JodBp6leWO/BJVxO3WUxhaUbDeV7ubrMQORhraMZ9szXgLxirmPmqmK8ku57D5Lo48BDrf0h4PGHnkWHWEDcwDHK+XlAHkslpuz1LflDqI0p8ONV/wRQ2a06PkEeAkclaQNT2GKzVzPCka+QPXG9r/rkCgYEA6fxAzPtZpiwKKXsfBHZiVuDx5Ce3ofG2ZmzzJA0APkez5W9pn5M3/EFkbSTFlb5DpQz7zbRILey+ckNJftRUYTcZqLvSz4fwpG/7Ns0lLlWgKdyWtb1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YRL5JWCi23/XsLmqwuAJTmEVdXLGx2Ra0OBXkWKuwck8hWxPRPx+ctWWQOKfn5bi8VaUqPYiuN3nrwkgJSPqQIDAQAB";

    public static String getQR(double allPrice) throws AlipayApiException {
//        Map<String,Object> resultMap=new HashMap<String, Object>();
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi-sandbox.dl.alipaydev.com/gateway.do",app_id,private_key,"json","GBK",alipay_public_key,"RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", TradeNumber.getUUID());
        bizContent.put("total_amount", allPrice);
        bizContent.put("subject", "测试商品");

//// 商品明细信息，按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息，按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);

//// 结算信息，按需传入
//JSONObject settleInfo = new JSONObject();
//JSONArray settleDetailInfos = new JSONArray();
//JSONObject settleDetail = new JSONObject();
//settleDetail.put("trans_in_type", "defaultSettle");
//settleDetail.put("amount", 0.01);
//settleDetailInfos.add(settleDetail);
//settleInfo.put("settle_detail_infos", settleDetailInfos);
//bizContent.put("settle_info", settleInfo);

//// 二级商户信息，按需传入
//JSONObject subMerchant = new JSONObject();
//subMerchant.put("merchant_id", "2088000603999128");
//bizContent.put("sub_merchant", subMerchant);

//// 业务参数信息，按需传入
//JSONObject businessParams = new JSONObject();
//businessParams.put("busi_params_key", "busiParamsValue");
//bizContent.put("business_params", businessParams);

//// 营销信息，按需传入
//JSONObject promoParams = new JSONObject();
//promoParams.put("promo_params_key", "promoParamsValue");
//bizContent.put("promo_params", promoParams);

        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            String qrcode = response.getQrCode();
            return qrcode;
        } else {
            System.out.println("调用失败");
            return null;
        }
//        System.out.println(response);

//        resultMap.put("qrcode",qrcode);
//        resultMap.put("outTradeNo","123");

    }
}
